﻿using AutoMapper;
using B.Z.BasicData.Domain.Entity.Authorize;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;

namespace B.Z.BasicData.API.Write.Application.User
{
    public class UserRemoveCommandHandler : IRequestHandler<UserRemoveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<T_User> userRepository;
        private readonly IMapper mapper;

        public UserRemoveCommandHandler(IBaseRepository<T_User> userRepository, IMapper mapper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(UserRemoveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnum.ok;
            result.Message = "删除成功";
            var user = mapper.Map<T_User>(request);
            user.IsDelete = true;
            user.UpdateTime = DateTime.Now;
            result.Data = await userRepository.Update(user);
            return await Task.FromResult(result);
        }
    }
}
